/**
 * 文件名：AppActivity.java
 * 全路径：com.mi.android.AppActivity
 */
package com.mi.android;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

/**
 * 功能：启动页<br>
 * 作者：蒋睿立<br>
 * 时间：2016年6月14日<br>
 * 版本：<br>
 * 
 */
public class AppActivity extends Activity {

	public class MessageReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Log.i(this.getClass().getName(), "action:" + intent.getAction());
			if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
				boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
				Log.i("connected", "connected state change to：" + String.valueOf(connected));
				showConnectState(connected);
			}
		}
	}

	private void showConnectState(boolean connected) {
		if (connected) {
			connectStateButton.setText(getString(R.string.running));
		} else {
			// Toast.makeText(this, "正在尝试连接服务器！！！", Toast.LENGTH_LONG).show();
			connectStateButton.setText(getString(R.string.stopped));
		}
	}

	private android.widget.Button connectStateButton;
	private ListView listView;
	private MessageReceiver mMessageReceiver;

	@Override
	public void finish() {
		moveTaskToBack(true);
		// super.finish();
	}

	public void jpushstate(android.view.View view) {

	}

	/**
	 * 启动《小米商城》
	 * 
	 * @param view
	 */
	public void launcher(android.view.View view) {
		Toast.makeText(this, "启动中...", Toast.LENGTH_LONG).show();
		Center.getInstance().launchMiShop(this);
	}

	/**
	 * 切换推送开关的事件
	 * 
	 * @param view
	 */
	public void push(android.view.View view) {
		boolean isStop = JPushInterface.isPushStopped(this);// 当前状态，true表示推送停止中
		setPushState(isStop);// 设置最新状态，true表示打开推送
	}

	private void registerMessageReceiver() {
		mMessageReceiver = new MessageReceiver();
		IntentFilter filter = new IntentFilter();
		filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
		filter.addAction("cn.jpush.android.intent.CONNECTION");
		filter.addCategory("com.mi.android");
		registerReceiver(mMessageReceiver, filter);
	}

	/**
	 * 设置推送状态
	 * 
	 * @param stopped
	 *            true表示开启推送，false表示关闭推送
	 */
	private void setPushState(boolean stopped) {
		boolean connected = JPushInterface.getConnectionState(this);
		Log.i("connected", "" + connected);
		if (stopped) {
			JPushInterface.resumePush(this);
		} else {
			JPushInterface.stopPush(this);
		}
		showConnectState(connected);
	}

	@Override
	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app);
		this.connectStateButton = (android.widget.Button) findViewById(R.id.push_btn);
		listView = (ListView) findViewById(R.id.listView1);
		ArrayList list = new ArrayList();
		list.add("LinearLayout");
		list.add("AbsoluteLayout");
		list.add("TableLayout");
		list.add("RelativeLayout");
		list.add("FrameLayout");
		list.add("LinearLayout");
		list.add("AbsoluteLayout");
		list.add("TableLayout");
		list.add("RelativeLayout");
		list.add("FrameLayout");
		ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
		listView.setAdapter(myArrayAdapter);
		
		JPushInterface.init(getApplicationContext());
		registerMessageReceiver();
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(mMessageReceiver);
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
		JPushInterface.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		JPushInterface.onResume(this);
		setPushState(true);
	}
}
